<?php

namespace App\Service\Index\v1_2_0;

use App\Model\CallBase;
use App\Model\CloseFriend;
use App\Model\Setting\SettingUserLevel;
use App\Model\UserProfile;
use App\Model\UserTag;
use App\Model\VoiceCall;

/**
 * 语音呼叫页面信息
 */
class VoiceCallingInfoService extends CallingBaseService
{
    protected function RoVerifyList()
    {
        return [
            'chat_user_id' => 'required|int',
            'is_caller' => 'required|int',
            'call_from' => 'required|int',
        ];
    }

    public function handle()
    {
        $userId = $this->getUserId();
        $chatUserId = $this->request->input('chat_user_id', 0);
        $callFrom = $this->request->input('call_from', 0);
        $isCaller = $this->request->input('is_caller', 1);
        $ret = $this->packInfo($chatUserId, $userId, $isCaller, $callFrom);
        return $ret;
    }

    /**
     * 语音呼叫页面信息
     */
    function packInfo($chatUserId = 0, $userId = 0, $isCaller = 1, $callFrom = 0)
    {
        //资费
        $callRecord = VoiceCall::checkRecord($userId, $chatUserId);
        if (!$callRecord) return [];
        $callRecord = $callRecord->toArray();
        $coinSetting = CallBase::getCallCoinSetting($callRecord, VoiceCall::CALL_TYPE);
        //资料
        $profile = UserProfile::getProfile($chatUserId);
        $posters = UserProfile::getPosters($chatUserId, $profile);
        array_unshift($posters['photo'], $profile->avatar);
        $coinText = $this->getCoinText(
            $coinSetting,
            $callFrom,
            $isCaller,
            VoiceCall::CALL_TYPE,
            $callRecord['from']
        );
        $ret = [
            'nick_name' => $profile->nick_name,
            'avatar' => $profile->avatar,
            'gender' => $profile->gender,
            'age' => $profile->age,
//            'city' => $profile->gender == 1 ? $profile->city : '保密',
            'city' => '',
            'level' => 1, //SettingUserLevel::getUserLevel($profile),
            'tags' => UserTag::getTagNames($chatUserId),
            'self_intro' => $profile->self_intro,
            'close_value' => CloseFriend::getCloseValue($userId, $chatUserId),
            'max_close_value' => 100,
            'call_tip' => $this->callTips('voice'),
            'coin_setting' => $coinText,
            'moment_pics' => $posters['photo']
        ];
        log_user_result($userId, "{$chatUserId}==" . json_encode($ret));
        return $ret;
    }
}
